package com.songshuai.pangu.api.controller;

import com.songshuai.pangu.api.service.SalesMeetingService;
import com.songshuai.webapi.baseapi.dto.feign.request.pangu.salemeeting.QueryCustomersReqDto;
import com.songshuai.webapi.baseapi.dto.feign.request.pangu.salemeeting.SendInvitationReqDto;
import com.songshuai.webapi.baseapi.dto.feign.request.pangu.salemeeting.TalkNotesReqDto;
import com.songshuai.webapi.baseapi.dto.feign.response.pangu.salemeeting.*;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 销售端会议对外提供接口
 * @Author: lhx
 * @Date: 2019/3/20 16:57
 */
@Slf4j
@Api("销售会议管理")
@RestController
@RequestMapping("/api/salemeeting/")
public class SalesMeetingController {




    @Autowired
    private SalesMeetingService salesMeetingService;



    /**
     * 查询会议列表
     * @param type 0-最近 1-未来
     * @param userId 当前登陆用户id
     * @return
     */
    @GetMapping("queryMeetingList")
    public QueryResponse<List<MeetingListResDto>> queryMeetingList(@RequestParam(value = "type",required = false,defaultValue = "0")Integer type,
                                                                   @RequestParam("userId")String userId){

        try {
            List<MeetingListResDto> meetingListResDtos = salesMeetingService.queryMeetingList(type,userId);
            if (null != meetingListResDtos){
                if (0 == type && !CollectionUtils.isEmpty(meetingListResDtos)){//如果是最近，盘古会返回所有，这里只取第一个
                    return QueryResponse.setResData(Arrays.asList(meetingListResDtos.get(0)));
                }
                return QueryResponse.setResData(meetingListResDtos);
            }
        } catch (Exception ex) {
            log.error("/api/salemeeting/queryMeetingList", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(),ex.getMessage());
        }
        return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED);
    }


    /**
     * 根据会议查询客户集合
     * @param userId 当前登陆人
     * @param code 会议编号
     * @return
     */
    @GetMapping("queryCustomerList")
    public QueryResponse<List<CustomerListResDto>> queryCustomerList(@RequestParam("userId")String userId,
                                                                     @RequestParam("code")String code){
        try {
            List<CustomerListResDto> resDtos = salesMeetingService.queryCustomerList(Integer.parseInt(userId), code);
            if (null != resDtos){
                return QueryResponse.setResData(resDtos);
            }
        } catch (Exception ex) {
            log.error("/api/salemeeting/queryCustomerList", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(),ex.getMessage());
        }
        return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED);
    }



    /**
     * 查看邀请函
     * @param code 会议编号
     * @return
     */
    @GetMapping("queryInvitationLetter")
    public QueryResponse<List<InvitationLetterResDto>> queryInvitationLetter(@RequestParam("code")String code){
        try {
            List<InvitationLetterResDto> invitationLetterResDto = salesMeetingService.queryInvitationLetter(code);
            if (null != invitationLetterResDto){
                return QueryResponse.setResData(invitationLetterResDto);
            }
        } catch (Exception ex) {
            log.error("/api/salemeeting/queryInvitationLetter", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(),ex.getMessage());
        }
        return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED);
    }


    /**
     * 查询客户信息
     * 邀约客户，根据姓名或手机号模糊查询
     * @return
     */
    @PostMapping("queryCustomers")
    public QueryResponse<QueryPageRes<List<CustomerInfosResDto>>> queryCustomers(@RequestBody BaseParmDto<QueryCustomersReqDto> request){

        try {
            QueryPageRes<List<CustomerInfosResDto>> listQueryPageRes = salesMeetingService.queryCustomers(request);
            if (null != listQueryPageRes){
                return QueryResponse.setResData(listQueryPageRes);
            }
        } catch (Exception ex) {
            log.error("/api/salemeeting/queryCustomers", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(),ex.getMessage());
        }
        return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED);
    }


    /**
     * 发送邀请函
     * @return
     */
    @PostMapping("sendInvitation")
    public QueryResponse sendInvitation(@RequestBody SendInvitationReqDto request){
        try {
            String invitation = salesMeetingService.sendInvitation(request);
            if(StringUtils.hasText(invitation)){
                return QueryResponse.setResData(invitation);
            }
        } catch (Exception ex) {
            log.error("/api/salemeeting/sendInvitation", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(),ex.getMessage());
        }
        return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
    }



    /**
     * 查看客户回执单信息
     * @param inviteNo 邀约编号
     * @return
     */
    @GetMapping("queryReceipt")
    public QueryResponse<ReceiptInfoResDto> queryReceipt(@RequestParam("inviteNo")String inviteNo){
        try {
            ReceiptInfoResDto receiptInfoResDto = salesMeetingService.queryReceipt(inviteNo);
            if (null != receiptInfoResDto){
                return QueryResponse.setResData(receiptInfoResDto);
            }
        } catch (Exception ex) {
            log.error("/api/salemeeting/queryReceipt", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(),ex.getMessage());
        }
        return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED);
    }


    /**
     * 查询销售人员信息
     * @param userId
     * @return
     */
    @GetMapping("querySalesInfo")
    public QueryResponse<List<SalesManInfoResDto>> querySalesInfo(@RequestParam("userId")String userId){
        try {
            List<SalesManInfoResDto> resDtos = salesMeetingService.querySalesInfo(userId);
            if (null != resDtos){
                return QueryResponse.setResData(resDtos);
            }
        } catch (Exception ex) {
            log.error("/api/salemeeting/querySalesInfo", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(),ex.getMessage());
        }
        return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED);

    }


    /**
     * 转交客户
     * @param userId 当前用户id
     * @param cluesIds 客户id集合
     * @param saleIds 转交销售id集合
     * @return
     */
    @PostMapping("handOverCustomer")
    public QueryResponse<Boolean> handOverCustomer(@RequestParam("userId")String userId,
                                          @RequestParam("cluesIds")List<Long> cluesIds,
                                          @RequestParam("saleId")List<Integer> saleIds){
        try {
            Boolean hand = salesMeetingService.handOverCustomer(userId, cluesIds, saleIds);
            if (hand){
                return QueryResponse.setResData(hand);
            }
        } catch (Exception ex) {
            log.error("/api/salemeeting/handOverCustomer", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(),ex.getMessage());
        }
        return new QueryResponse(ResultStatus.FAILOfficial);
    }


    /**
     * 新增沟通记录
     * @return
     */
    @PostMapping("addTalkNotes")
    public QueryResponse<Boolean> talkNotes(@RequestBody TalkNotesReqDto request){
        try {
            Boolean hand = salesMeetingService.addTalkNotes(request);
            if (hand){
                return QueryResponse.setResData(hand);
            }
        } catch (Exception ex) {
            log.error("/api/salemeeting/addTalkNotes", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(),ex.getMessage());
        }
        return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
    }


    /**
     * 获取客户历史沟通记录
     * @param clueGuid
     * @return
     */
    @GetMapping("getCommunicateList")
    public QueryResponse<List<CommunicateListResDto>> getCommunicateList(@RequestParam("clueGuid")Long clueGuid){
        try {
            List<CommunicateListResDto> resDtos = salesMeetingService.getCommunicateList(clueGuid);
            if (null != resDtos){
                return QueryResponse.setResData(resDtos);
            }
        } catch (Exception ex) {
            log.error("/api/salemeeting/getCommunicateList", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(),ex.getMessage());
        }
        return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED);
    }


}
